package com.pengtu.gsj.controller.jsbz;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pengtu.gsj.controller.base.DefaultBaseController;
import com.pengtu.gsj.dao.base.Page;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.entity.jsbz.RoadCycle;
import com.pengtu.gsj.interceptor.Token;
import com.pengtu.gsj.service.jsbz.RoadCycleService;
import com.pengtu.gsj.utils.MathsUtils;

@Controller
@RequestMapping("roadCycle")
public class RoadCycleController extends DefaultBaseController{
	@Autowired
	private RoadCycleService roadCycleService;
	
	
	
	/**
	 * 加载
	 * @param id
	 * @return
	 */
	@ModelAttribute
	public RoadCycle load(String id){
		RoadCycle roadCycle;
		if(StringUtils.isNotEmpty(id)){
			roadCycle = roadCycleService.getRoadCycleById(id);
		}else{
			roadCycle = new RoadCycle();
		}
		return roadCycle;
	}
	
	
	/**
	 * 页面跳转
	 * @return
	 */
	@RequestMapping("listInfo")
	@Token(save = true)
	public String showLisfInfo() {
		return "jsbz/roadCycle_list";
	}
	
	
	/**
	 * 
	 * getRoadMaterialList:
	 * 适用:
	 * @param page
	 * @param request
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping(value="list", produces = "application/json; charset=utf-8")
	@ResponseBody
	public Page<RoadCycle> getRoadMaterialList(Page<RoadCycle> page, HttpServletRequest request){
		QueryFilter queryFilter = QueryFilter.buildFromHttpRequest(request);
		if (!page.isOrderBySetted()) {
			page.setOrderBy("testYear");
			page.setOrder(Page.ASC);
		}
		page = roadCycleService.findRoadCycle(page,queryFilter);
		return page;
	}
	
	/**
	 * 保存
	 * @param roadMaterial
	 * @return
	 */
	@RequestMapping(value = "save", method=RequestMethod.POST)
	@ResponseBody
	@Token(remove = true)
	public boolean saveRoadCycle(@ModelAttribute RoadCycle roadCycle){
		//计算沥青砼材料利用率、水泥砼材料利用率、综合利用率
		roadCycle.setAsphalRate(MathsUtils.divDouble((roadCycle.getAsphalLocalUse()+roadCycle.getAsphalCycleUse())*100, roadCycle.getAsphalMaterial(), 2));
		roadCycle.setCementRate(MathsUtils.divDouble((roadCycle.getCementLocalUse()+roadCycle.getCementCycleUse())*100, roadCycle.getCementMaterial(), 2));
		roadCycle.setWholeRate(MathsUtils.divDouble((roadCycle.getAsphalLocalUse()+roadCycle.getAsphalCycleUse()+roadCycle.getCementLocalUse()+roadCycle.getCementCycleUse())*100, (roadCycle.getAsphalMaterial()+roadCycle.getCementMaterial()), 2));
		roadCycle.setUseNumber(MathsUtils.formatDouble((roadCycle.getAsphalLocalUse() + roadCycle.getAsphalCycleUse() + roadCycle.getCementLocalUse() + roadCycle.getCementCycleUse()),2));
		roadCycleService.saveRoadCycle(roadCycle);
		return true;
	}
	
	
	/**
	 * 加载数据
	 * @param infoManagement
	 * @return
	 */
	@RequestMapping(value="show")
	@ResponseBody                                                     
	public RoadCycle showRoadCycle (@ModelAttribute RoadCycle roadCycle){
		return roadCycle;
	}
	
	/**
	 * 删除
	 * @param infoManagement
	 * @return
	 */
	@RequestMapping(value="delete")
	@ResponseBody
	public boolean delateInfoManagement(@ModelAttribute RoadCycle roadCycle){
		roadCycleService.delateRoadCycle(roadCycle);
		return true;
	}
	
}
